﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Reflection;
using Sebarf.Diagnostics.Interfaces;
using Sebarf.Utils.Interfaces;

#endregion

namespace Sebarf.Services.Interfaces {
	/// <summary>
	/// Basic implementation of <see cref="IService"/>
	/// </summary>
	[Serializable]
	public class Service : IServiceImpl, IMonitorableService {
		#region Public Properties

		/// <summary>
		/// Gets or sets the friendly name of the service
		/// </summary>
		public string FriendlyName { get; set; }

		[ServiceRequest]
		public IObjectHierarchyFlattenerService ObjectHierarchyFlattenerService { get; set; }

		#endregion

		#region Protected Methods

		protected virtual ServiceStatistics OnCollectStatisticData(ServiceStatistics statisticData) {
			return statisticData;
		}

		#endregion

		#region IMonitorableService Members

		public ServiceStatistics CollectAndGetServiceStatistics() {
			return
				OnCollectStatisticData(new ServiceStatistics(this, ObjectHierarchyFlattenerService) { FriendlyServiceName = FriendlyName });
		}

		#endregion

		#region IServiceImpl Members

		/// <summary>
		/// checks if all properties that request a service or a context are set
		/// <see cref="ServiceRequest"/> and <see cref="InterLinqContextRequest"/>
		/// </summary>
		public virtual void DoSetup() {
			Logger.WriteDebug("Setup:" + ToString());
			// inject services
			foreach (PropertyInfo property in ObjectHierarchyFlattenerService.GetAllProperties(GetType())) {
				foreach (Attribute attribute in property.GetCustomAttributes(true)) {
					if (attribute is ServiceRequest) {
						if (!(attribute as ServiceRequest).IsOptional) {
							Check.ArgumentNotNull(property.GetValue(this, null), "service not found:" + property.PropertyType);
						}
					}
				}
			}
		}

		public virtual void DoShutdown() {
			Logger.WriteDebug("Shutdown:" + ToString());
		}

		#endregion
	}
}